package com.quanyan.place.wrapper.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.place.component.MerchantClientPayType;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.TbAdminUser;
import com.quanyan.place.entity.TbBizPlaceOrder;
import com.quanyan.place.entity.TbCommonParameter;
import com.quanyan.place.entity.apireq.ReqPlaceGoodsCommonVo;
import com.quanyan.place.entity.apireq.ReqPlaceGoodsTypeVo;
import com.quanyan.place.entity.apireq.ReqPlaceGoodsVo;
import com.quanyan.place.entity.apireq.ReqSupplierVo;
import com.quanyan.place.entity.apiresp.*;
import com.quanyan.place.entity.vo.PlaceFinanceAuditVo;
import com.quanyan.place.entity.vo.PlacePrintVo;
import com.quanyan.place.entity.vo.PlaceScanOrderPayVo;
import com.quanyan.place.mapper.TbBizPlaceOrderManagerMapper;
import com.quanyan.place.mapper.TbCommonParameterMapper;
import com.quanyan.place.service.*;
import com.quanyan.place.utils.PlaceUtil;
import com.quanyan.place.wrapper.PlaceGoodsWrapper;
import com.quanyan.place.wrapper.PlaceOrderServiceWrapper;
import org.apache.commons.collections.MapUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 场馆会员wrapper实现
 * Created by yangyun on 16-8-25.
 */
@Service("PlaceGoodsWrapper")
public class PlaceGoodsWrapperImpl implements PlaceGoodsWrapper{
    private static final Logger logger = LoggerFactory.getLogger(PlaceGoodsWrapperImpl.class);

    @Autowired
    PlaceGoodsService placeGoodsService;
    @Autowired
    PlaceMemberService placeMemberService;
    @Autowired
    PlaceOrderService placeOrderService;
    @Autowired
    TbBizPlaceOrderManagerMapper tbBizPlaceOrderManagerMapper;
    @Autowired
    CacheService cacheService;
    @Autowired
    TbCommonParameterMapper tbCommonParameterMapper;
    @Autowired
    BaseService baseService;
    @Autowired
    PlaceOrderUtilService placeOrderUtilService;
    @Autowired
    FinanceService financeService;

    /**
     * 商品列表
     * @param reqPlaceGoodsVo
     * @return
     */
    @Override
    public APIResponse getPlaceGoodsList(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return APIResponse.returnSuccess(placeGoodsService.getPlaceGoodsList(reqPlaceGoodsVo));
    }

    public APIResponse getPlaceGoodsListNew(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return APIResponse.returnSuccess(placeGoodsService.getPlaceGoodsListNew(reqPlaceGoodsVo));
    }



    /**
     * 新增商品信息
     * @param reqPlaceGoodsVo
     * @return
     */
   /* @Override
    public APIResponse insertPlaceGoodsInfo(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return  placeGoodsService.insertPlaceGoodsInfo(reqPlaceGoodsVo);
    }*/

    @Override
    public APIResponse insertInventoryRecord(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return placeGoodsService.insertInventoryRecord(reqPlaceGoodsVo);
    }

    /**
     * 新增商品信息
     * @param reqPlaceGoodsVo
     * @return
     */
    @Override
    public APIResponse insertPlaceGoodsInfo(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return  placeGoodsService.insertPlaceGoodsInfo(reqPlaceGoodsVo);
    }

    @Override
    public APIResponse insertPlaceGoodsInfoNew(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return  placeGoodsService.insertPlaceGoodsInfoNew(reqPlaceGoodsVo);
    }

    /**
     * 更新商品信息
     * @param reqPlaceGoodsVo
     * @return
     */
    @Override
    public APIResponse updatePlaceGoodsInfo(ReqPlaceGoodsVo reqPlaceGoodsVo) {
       return  placeGoodsService.updatePlaceGoodsInfo(reqPlaceGoodsVo);
    }

    @Override
    public APIResponse updatePlaceGoodsInfoNew(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return  placeGoodsService.updatePlaceGoodsInfoNew(reqPlaceGoodsVo);
    }

    /**
     * 删除商品信息
     * @param reqPlaceGoodsVo
     * @return
     */
    @Override
    public void deletePlaceGoodsInfo(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        placeGoodsService.deletePlaceGoodsInfo(reqPlaceGoodsVo);
    }

    /**
     * 导出商品信息
     * @param reqPlaceGoodsVo
     * @return
     */
    @Override
    public APIResponse exportPlaceGoodsInfo(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return APIResponse.returnSuccess(placeGoodsService.exportPlaceGoodsInfo(reqPlaceGoodsVo));
    }

    @Override
    public APIResponse exportPlaceGoodsInfoNew(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return APIResponse.returnSuccess(placeGoodsService.exportPlaceGoodsInfo(reqPlaceGoodsVo));
    }

    /**
     *导入商品信息
     * @param reqPlaceGoodsVo
     * @return
     */
    @Override
    public APIResponse importPlaceGoodsInfo(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return placeGoodsService.importPlaceGoodsInfo(reqPlaceGoodsVo);
    }

    /**
     * 新增商品类型
     * @param reqPlaceGoodsTypeVo
     * @return
     */
    @Override
    public APIResponse insertPlaceGoodsTypeData(ReqPlaceGoodsTypeVo reqPlaceGoodsTypeVo) {
        return placeGoodsService.insertPlaceGoodsTypeData(reqPlaceGoodsTypeVo);
    }

    /**
     * 获取商品类型
     * @param reqPlaceGoodsTypeVo
     * @return
     */
    @Override
    public APIResponse getPlaceGoodsTypeData(ReqPlaceGoodsTypeVo reqPlaceGoodsTypeVo) {
        return APIResponse.returnSuccess(placeGoodsService.getPlaceGoodsTypeData(reqPlaceGoodsTypeVo));
    }

    /**
     * 获取商品类型（分页）
     * @param reqPlaceGoodsTypeVo
     * @return
     */
   /* @Override
    public APIResponse getPlaceGoodsTypeList(ReqPlaceGoodsTypeVo reqPlaceGoodsTypeVo) {
        return APIResponse.returnSuccess(placeGoodsService.getPlaceGoodsTypeList(reqPlaceGoodsTypeVo));
    }
*/
    /**
     * 获取商品类型（分页）
     * @param reqPlaceGoodsTypeVo
     * @return
     */
    @Override
    public APIResponse getPlaceGoodsTypeList(ReqPlaceGoodsTypeVo reqPlaceGoodsTypeVo) {
        return APIResponse.returnSuccess(placeGoodsService.getPlaceGoodsTypeList(reqPlaceGoodsTypeVo));
    }

    /**
     * 更新商品类型
     * @param reqPlaceGoodsTypeVo
     * @return
     */
    @Override
    public APIResponse updatePlaceGoodsTypeData(ReqPlaceGoodsTypeVo reqPlaceGoodsTypeVo) {
       return placeGoodsService.updatePlaceGoodsTypeData(reqPlaceGoodsTypeVo);
    }

  /*  @Override
    public APIResponse updatePlaceGoodsTypeDatassss(ReqPlaceGoodsTypeVo reqPlaceGoodsTypeVo) {
        return placeGoodsService.updatePlaceGoodsTypeDatassss(reqPlaceGoodsTypeVo);
    }*/

    /**
     * 提交商品订单
     * @param reqPlaceGoodsVo
     * @return
     */

    @Override
    public APIResponse submitGoodsOrder(ReqPlaceGoodsVo reqPlaceGoodsVo, PlaceOrderServiceWrapper placeOrderServiceWrapper) {
        APIResponse apiResponse = null;
        boolean isScanPayFlag = false;
        if(MerchantClientPayType.getMatchScanPayType().containsKey(reqPlaceGoodsVo.getAdminPayType().toString())){
            isScanPayFlag = true;
        }

        boolean isCompanyFlag = false;//对公转账
        if (PlaceConstants.PLACE_ADMIN_PAY_WAYS_COMPANY.equals(reqPlaceGoodsVo.getAdminPayType().toString())){//对公转账支付方式匹配
            isCompanyFlag = true;
        }
        try {
            if (isScanPayFlag) {
                APIResponse orderResponse = placeGoodsService.submitGoodsOrderForScanPay(reqPlaceGoodsVo, placeOrderServiceWrapper);
                if (orderResponse.isRet() && orderResponse.getData() != null) {
                    TbBizPlaceOrder tbBizPlaceOrder = (TbBizPlaceOrder) orderResponse.getData();
                    PlaceScanOrderPayVo placeScanOrderPayVo = new PlaceScanOrderPayVo();
                    placeScanOrderPayVo.setReceivedAmount(reqPlaceGoodsVo.getReceivedAmount());
                    placeScanOrderPayVo.setAdminPayType(reqPlaceGoodsVo.getAdminPayType().toString());
                    placeScanOrderPayVo.setScanCode(reqPlaceGoodsVo.getScanCode());
                    placeScanOrderPayVo.setTbBizPlaceOrder(tbBizPlaceOrder);
                    placeScanOrderPayVo.setTbBizPlacePrivateOrder(null);
                    placeScanOrderPayVo.setReqTicketPay(null);
                    apiResponse = placeOrderService.scanOrderPay(placeScanOrderPayVo);
                } else {
                    return APIResponse.returnFail("扫码购买商品生成订单失败");
                }
            } else if (isCompanyFlag) {
                APIResponse orderResponse = placeGoodsService.submitGoodsOrderForScanPay(reqPlaceGoodsVo, placeOrderServiceWrapper);
                if (orderResponse.isRet() && orderResponse.getData() != null) {
                    TbBizPlaceOrder tbBizPlaceOrder = (TbBizPlaceOrder)orderResponse.getData();

                    //插入审核记录
                    PlaceFinanceAuditVo placeFinanceAuditVo = placeOrderUtilService.tbBizPlaceOrder2PlaceFinanceAuditVo(tbBizPlaceOrder);
                    if (!StringUtils.isEmpty(placeFinanceAuditVo)){
                        financeService.createAuditRecord(placeFinanceAuditVo);
                    }

                    //打印小票
                    PlacePrintVo placePrintVo = new PlacePrintVo();
                    if (!StringUtils.isEmpty(reqPlaceGoodsVo.getMemberCardId())){
                        placePrintVo.setIsMember(PlaceConstants.YES);
                    }else {
                        placePrintVo.setIsMember(PlaceConstants.NO);
                    }
                    placePrintVo.setBusinessType(PlaceConstants.ORDER_USE_GOODS);
                    placePrintVo.setPlaceOrderId(tbBizPlaceOrder.getPlaceOrderId());
                    return APIResponse.returnSuccess(placeOrderUtilService.getPrintOrderInfo(placePrintVo));
                } else {
                    return APIResponse.returnFail("对公转账生成订单失败");
                }
            }else {
                apiResponse = placeGoodsService.submitGoodsOrder(reqPlaceGoodsVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return APIResponse.returnFail(e.getMessage());
        }
        return apiResponse;
    }

    /**
     * 商品收入报表
     * @param reqPlaceGoodsVo
     * @return
     */
    @Override
    public APIResponse goodsSaleIncomeReport(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return APIResponse.returnSuccess(placeGoodsService.goodsSaleIncomeReport(reqPlaceGoodsVo));
    }

    @Override
    public APIResponse inventoryRecordList(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return APIResponse.returnSuccess(placeGoodsService.inventoryRecordList(reqPlaceGoodsVo));
    }

    /**
     * 商品售卖会员卡消费获取用户所有相关会员卡
     */
    @Override
    public APIResponse<List<RespPlaceMemberCard>> goodsConsumeGetMemberCardList(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return placeMemberService.goodsConsumeGetMemberCardList(null,reqPlaceGoodsVo.getMobile(),reqPlaceGoodsVo.getPlaceId(),null);
    }


    @Override
    public APIResponse getOneLevel(String dataKey) {
        return APIResponse.returnSuccess(placeGoodsService.getOneLevel(dataKey));
    }

    @Override
    public APIResponse getTypeByParenLevel(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return APIResponse.returnSuccess(placeGoodsService.getTypeByParenLevel(reqPlaceGoodsVo));
    }


    /**
     * 商品销售明细
     *
     * @param map
     * @return
     */
    public APIResponse<PageObj<RespPlaceGoodsDetailSum>> getGoodsDetail(@Param("map") Map<String, Object> map){
        TbCommonParameter parameter = baseService.getParameterByKey("PLACE_LIMIT_DATE");
        String limitDate = parameter!=null && parameter.getParameterValue() != null ? parameter.getParameterValue() : "2017-04-29";
        map.put("placeLimitDate",limitDate);
        Integer pageNum = Constants.DEFAULT_PAGE_NUM;
        Integer pageSize = Constants.DEFAULT_PAGE_SIZE;
        Integer reqPageNum = MapUtils.getInteger(map, "pageNum");
        if (!StringUtils.isEmpty(MapUtils.getInteger(map, "pageSize"))) {
            pageSize = MapUtils.getInteger(map, "pageSize");
        }
        if (!StringUtils.isEmpty(MapUtils.getInteger(map, "pageNum"))) {
            pageNum = MapUtils.getInteger(map, "pageNum") * pageSize;
        }
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        Integer total = tbBizPlaceOrderManagerMapper.countGetGoodsDetail(map);
        List<RespPlaceGoodsDetail>  goodsDetailList = tbBizPlaceOrderManagerMapper.getGoodsDetail(map);
        for (RespPlaceGoodsDetail resp : goodsDetailList){
            if(resp.getCreateUid()!=null&&resp.getCreateUid()!=0){
                TbAdminUser user = cacheService.getAdminUser(resp.getCreateUid());
                if(user!=null){
                    resp.setCreateName(user.getTrueName());
                }
            }
        }
        RespPlaceGoodsDetailSum sum = new RespPlaceGoodsDetailSum();
        sum.setRespPlaceGoodsDetailList(goodsDetailList);
        Integer goodsCountSum = tbBizPlaceOrderManagerMapper.getGoodsDetailCountSum(map);
        BigDecimal amountSum = tbBizPlaceOrderManagerMapper.getGoodsDetailAmountSum(map);
        BigDecimal totalCostPriceSum = tbBizPlaceOrderManagerMapper.getGoodsDetailCostSum(map);
        sum.setAmountSum(amountSum);
        sum.setTotalGoodsCountSum(goodsCountSum);
        sum.setLimit("说明："+limitDate+"之前下的订单数据不支持查询");
        PageObj<RespPlaceGoodsDetailSum> result = PageObj.create(total, reqPageNum, pageSize, sum);
        return  APIResponse.returnSuccess(result);
    }

    /**
     * 商品销售明细导出
     *
     * @param map
     * @return
     */
    public APIResponse<RespPlaceGoodsDetailSum> getGoodsDetailExport(@Param("map") Map<String, Object> map){
        TbCommonParameter parameter = baseService.getParameterByKey("PLACE_LIMIT_DATE");
        String limitDate = parameter!=null && parameter.getParameterValue() != null ? parameter.getParameterValue() : "2017-04-29";
        map.put("placeLimitDate",limitDate);
        map.put("pageNum",null);
        map.put("pageSize",null);
        List<RespPlaceGoodsDetail>  goodsDetailList = tbBizPlaceOrderManagerMapper.getGoodsDetailExport(map);
        for (RespPlaceGoodsDetail resp : goodsDetailList){
            if(resp.getCreateUid()!=null&&resp.getCreateUid()!=0){
                TbAdminUser user = cacheService.getAdminUser(resp.getCreateUid());
                if(user!=null){
                    resp.setCreateName(user.getTrueName());
                }
            }
            resp.setCouponDiscount(PlaceUtil.isEmpty(resp.getDiscountCoupon()).add(PlaceUtil.isEmpty(resp.getDiscountDiscountCoupon())));
            resp.setMemberDiscount(PlaceUtil.isEmpty(resp.getDiscountMember()).add(PlaceUtil.isEmpty(resp.getDiscountDiscountMember())));
        }
        RespPlaceGoodsDetailSum sum = new RespPlaceGoodsDetailSum();
        sum.setRespPlaceGoodsDetailList(goodsDetailList);
        Integer goodsCountSum = tbBizPlaceOrderManagerMapper.getGoodsDetailCountSum(map);
        BigDecimal amountSum = tbBizPlaceOrderManagerMapper.getGoodsDetailAmountSum(map);
        BigDecimal totalCostPriceSum = tbBizPlaceOrderManagerMapper.getGoodsDetailCostSum(map);
        sum.setAmountSum(amountSum);
        sum.setTotalGoodsCountSum(goodsCountSum);
        sum.setTotalCostPriceSum(totalCostPriceSum);
        sum.setLimit("说明："+limitDate+"之前下的订单数据不支持查询");
        return  APIResponse.returnSuccess(sum);
    }


    @Override
    public APIResponse saveSupplierInfo(ReqSupplierVo reqSupplierVo) {
       return placeGoodsService.saveSupplierInfo(reqSupplierVo);
    }

    @Override
    public APIResponse getSupplierInfoList(ReqSupplierVo reqSupplierVo) {
        return APIResponse.returnSuccess(placeGoodsService.getSupplierInfoList(reqSupplierVo));
    }

    @Override
    public APIResponse deleteSupplierInfo(ReqSupplierVo reqSupplierVo) {
        return APIResponse.returnSuccess(placeGoodsService.deleteSupplierInfo(reqSupplierVo));
    }

    @Override
    public APIResponse updateSupplierInfo(ReqSupplierVo reqSupplierVo) {
        return APIResponse.returnSuccess(placeGoodsService.updateSupplierInfo(reqSupplierVo));
    }

    @Override
    public APIResponse getSupplierNameByCityId(Integer cityId) {
        return APIResponse.returnSuccess(placeGoodsService.getSupplierNameByCityId(cityId));
    }

    @Override
    public APIResponse checkGoodsInventory(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return APIResponse.returnSuccess(placeGoodsService.checkGoodsInventory(reqPlaceGoodsVo));
    }

    @Override
    public APIResponse goodsTradeRecord(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return APIResponse.returnSuccess(placeGoodsService.goodsTradeRecord(reqPlaceGoodsVo));
    }


    /**
     * 商品销售统计 -商品编号
     * @param map
     * @return
     */
    public APIResponse<?> getGoodsStatisticsByGoodsId(@Param("map")Map<String, Object> map) {
        TbCommonParameter parameter = baseService.getParameterByKey("PLACE_LIMIT_DATE");
        String limitDate = parameter!=null && parameter.getParameterValue() != null ? parameter.getParameterValue() : "2017-04-29";
        map.put("placeLimitDate",limitDate);
        Integer pageNum = Constants.DEFAULT_PAGE_NUM;
        Integer pageSize = Constants.DEFAULT_PAGE_SIZE;
        Integer reqPageNum = MapUtils.getInteger(map, "pageNum");
        if (!StringUtils.isEmpty(MapUtils.getInteger(map, "pageSize"))) {
            pageSize = MapUtils.getInteger(map, "pageSize");
        }
        if (!StringUtils.isEmpty(MapUtils.getInteger(map, "pageNum"))) {
            pageNum = MapUtils.getInteger(map, "pageNum") * pageSize;
        }
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        List<RespGoodsStatisticsGoodsId>  respGoodsId = tbBizPlaceOrderManagerMapper.getGoodsStatisticsByGoodsId(map);
        Integer total = tbBizPlaceOrderManagerMapper.countGetGoodsStatisticsByGoodsId(map);
        RespGoodsStatisticsGoodsIdSum sum = new RespGoodsStatisticsGoodsIdSum();
        sum.setRespGoodsId(respGoodsId);
        Integer goodsCountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailCountSum(map);
        BigDecimal amountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailAmountSum(map);
        sum.setGoodsCountTotal(goodsCountTotal);
        sum.setAmountTotal(amountTotal);
        PageObj<RespGoodsStatisticsGoodsIdSum> result = PageObj.create(total, reqPageNum, pageSize, sum);
        return APIResponse.returnSuccess(result);
    }

    /**
     * 商品销售统计 -商品编号(导出)
     * @param map
     * @return
     */
    public APIResponse<?> getGoodsStatisticsByGoodsIdExport(@Param("map")Map<String, Object> map) {
        TbCommonParameter parameter = baseService.getParameterByKey("PLACE_LIMIT_DATE");
        String limitDate = parameter!=null && parameter.getParameterValue() != null ? parameter.getParameterValue() : "2017-04-29";
        map.put("placeLimitDate",limitDate);
        map.put("pageNum",null);
        map.put("pageSize",null);
        List<RespGoodsStatisticsGoodsId>  respGoodsId = tbBizPlaceOrderManagerMapper.getGoodsStatisticsByGoodsId(map);
        RespGoodsStatisticsGoodsIdSum sum = new RespGoodsStatisticsGoodsIdSum();
        sum.setRespGoodsId(respGoodsId);
        Integer goodsCountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailCountSum(map);
        BigDecimal amountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailAmountSum(map);
        sum.setGoodsCountTotal(goodsCountTotal);
        sum.setAmountTotal(amountTotal);
        return APIResponse.returnSuccess(sum);
    }

    /**
     * 商品销售统计 -一级目录
     * @param map
     * @return
     */
    public APIResponse<?> getGoodsStatisticsByOneLevel(@Param("map") Map<String, Object> map) {
        TbCommonParameter parameter = baseService.getParameterByKey("PLACE_LIMIT_DATE");
        String limitDate = parameter!=null && parameter.getParameterValue() != null ? parameter.getParameterValue() : "2017-04-29";
        map.put("placeLimitDate",limitDate);
        Integer pageNum = Constants.DEFAULT_PAGE_NUM;
        Integer pageSize = Constants.DEFAULT_PAGE_SIZE;
        Integer reqPageNum = MapUtils.getInteger(map, "pageNum");
        if (!StringUtils.isEmpty(MapUtils.getInteger(map, "pageSize"))) {
            pageSize = MapUtils.getInteger(map, "pageSize");
        }
        if (!StringUtils.isEmpty(MapUtils.getInteger(map, "pageNum"))) {
            pageNum = MapUtils.getInteger(map, "pageNum") * pageSize;
        }
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        List<RespGoodsStatisticsOneLevel> respOneLevel = tbBizPlaceOrderManagerMapper.getGoodsStatisticsByOneLevel(map);
        Integer total = tbBizPlaceOrderManagerMapper.countGetGoodsStatisticsByOneLevel(map);
        RespGoodsStatisticsOneLevelSum sum = new RespGoodsStatisticsOneLevelSum();
        Integer goodsCountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailCountSum(map);
        BigDecimal amountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailAmountSum(map);
        List<RespGoodsStatisticsOneLevel> respOneLevelNew = new ArrayList<>();
        DecimalFormat dt = new DecimalFormat("#0.00%");//设置格式化对象
        int index = 0;//下标
        BigDecimal laseAmountProportion = new BigDecimal(0);//最后实付金额比例
        BigDecimal laseCountProportion = new BigDecimal(0);//最后数量比例
        BigDecimal totalProportion = new BigDecimal(1);//100%
        for (RespGoodsStatisticsOneLevel rep : respOneLevel) {
            index++;
            BigDecimal countTemp = goodsCountTotal ==0 ? BigDecimal.ZERO : new BigDecimal(rep.getGoodsCountSum()).divide(new BigDecimal(goodsCountTotal),4, RoundingMode.HALF_EVEN);
            BigDecimal amountTemp = amountTotal.equals(BigDecimal.ZERO.setScale(2))?BigDecimal.ZERO:rep.getAmountSum().divide(amountTotal,4, RoundingMode.HALF_EVEN);
            rep.setAmountProportion(dt.format(amountTemp));
            rep.setGoodsCountProportion(dt.format(countTemp));
            if (index == respOneLevel.size()){
                rep.setAmountProportion(dt.format(totalProportion.subtract(laseAmountProportion)));
                rep.setGoodsCountProportion(dt.format(totalProportion.subtract(laseCountProportion)));
            }else{
                laseAmountProportion = laseAmountProportion.add(amountTemp);
                laseCountProportion = laseCountProportion.add(countTemp);
            }
            if(goodsCountTotal==0){
                rep.setGoodsCountProportion(dt.format(0));
            }
            if(amountTotal.equals(BigDecimal.ZERO.setScale(2))){
                rep.setAmountProportion(dt.format(0));
            }
            respOneLevelNew.add(rep);
        }
        sum.setRespOneLevel(respOneLevelNew);
        sum.setGoodsCountTotal(goodsCountTotal);
        sum.setAmountTotal(amountTotal);
        PageObj<RespGoodsStatisticsOneLevelSum> result = PageObj.create(total, reqPageNum, pageSize, sum);
        return APIResponse.returnSuccess(result);
    }

    /**
     * 商品销售统计 -一级目录(导出)
     * @param map
     * @return
     */
    public APIResponse<?> getGoodsStatisticsByOneLevelExport(@Param("map") Map<String, Object> map) {
        TbCommonParameter parameter = baseService.getParameterByKey("PLACE_LIMIT_DATE");
        String limitDate = parameter!=null && parameter.getParameterValue() != null ? parameter.getParameterValue() : "2017-04-29";
        map.put("placeLimitDate",limitDate);
        map.put("pageNum",null);
        map.put("pageSize",null);
        List<RespGoodsStatisticsOneLevel> respOneLevel = tbBizPlaceOrderManagerMapper.getGoodsStatisticsByOneLevel(map);
        RespGoodsStatisticsOneLevelSum sum = new RespGoodsStatisticsOneLevelSum();
        Integer goodsCountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailCountSum(map);
        BigDecimal amountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailAmountSum(map);
        List<RespGoodsStatisticsOneLevel> respOneLevelNew = new ArrayList<>();
        DecimalFormat dt = new DecimalFormat("#0.00%");//设置格式化对象
        int index = 0;//下标
        BigDecimal laseAmountProportion = new BigDecimal(0);//最后实付金额比例
        BigDecimal laseCountProportion = new BigDecimal(0);//最后数量比例
        BigDecimal totalProportion = new BigDecimal(1);//100%
        for (RespGoodsStatisticsOneLevel rep : respOneLevel) {
            index++;
            BigDecimal countTemp = goodsCountTotal ==0 ? BigDecimal.ZERO : new BigDecimal(rep.getGoodsCountSum()).divide(new BigDecimal(goodsCountTotal),4, RoundingMode.HALF_EVEN);
            BigDecimal amountTemp = amountTotal.equals(BigDecimal.ZERO.setScale(2))?BigDecimal.ZERO:rep.getAmountSum().divide(amountTotal,4, RoundingMode.HALF_EVEN);
            rep.setAmountProportion(dt.format(amountTemp));
            rep.setGoodsCountProportion(dt.format(countTemp));
            if (index == respOneLevel.size()){
                rep.setAmountProportion(dt.format(totalProportion.subtract(laseAmountProportion)));
                rep.setGoodsCountProportion(dt.format(totalProportion.subtract(laseCountProportion)));
            }else{
                laseAmountProportion = laseAmountProportion.add(amountTemp);
                laseCountProportion = laseCountProportion.add(countTemp);
            }
            if(goodsCountTotal==0){
                rep.setGoodsCountProportion(dt.format(0));
            }
            if(amountTotal.equals(BigDecimal.ZERO.setScale(2))){
                rep.setAmountProportion(dt.format(0));
            }
            respOneLevelNew.add(rep);
        }
        sum.setRespOneLevel(respOneLevelNew);
        sum.setGoodsCountTotal(goodsCountTotal);
        sum.setAmountTotal(amountTotal);
        return APIResponse.returnSuccess(sum);
    }

    /**
     * 商品销售统计 -二级目录
     * @param map
     * @return
     */
    public APIResponse<?> getGoodsStatisticsByTwoLevel(@Param("map") Map<String, Object> map) {
        TbCommonParameter parameter = baseService.getParameterByKey("PLACE_LIMIT_DATE");
        String limitDate = parameter!=null && parameter.getParameterValue() != null ? parameter.getParameterValue() : "2017-04-29";
        map.put("placeLimitDate",limitDate);
        Integer pageNum = Constants.DEFAULT_PAGE_NUM;
        Integer pageSize = Constants.DEFAULT_PAGE_SIZE;
        Integer reqPageNum = MapUtils.getInteger(map, "pageNum");
        if (!StringUtils.isEmpty(MapUtils.getInteger(map, "pageSize"))) {
            pageSize = MapUtils.getInteger(map, "pageSize");
        }
        if (!StringUtils.isEmpty(MapUtils.getInteger(map, "pageNum"))) {
            pageNum = MapUtils.getInteger(map, "pageNum") * pageSize;
        }
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        List<RespGoodsStatisticsTwoLevel> respTwoLevel = tbBizPlaceOrderManagerMapper.getGoodsStatisticsByTwoLevel(map);
        Integer total = tbBizPlaceOrderManagerMapper.countGetGoodsStatisticsByTwoLevel(map);
        RespGoodsStatisticsTwoLevelSum sum = new RespGoodsStatisticsTwoLevelSum();
        Integer goodsCountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailCountSum(map);
        BigDecimal amountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailAmountSum(map);
        List<RespGoodsStatisticsTwoLevel> respTwoLevelNew = new ArrayList<>();
        DecimalFormat dt = new DecimalFormat("#0.00%");//设置格式化对象
        int index = 0;//下标
        BigDecimal laseAmountProportion = new BigDecimal(0);//最后实付金额比例
        BigDecimal laseCountProportion = new BigDecimal(0);//最后数量比例
        BigDecimal totalProportion = new BigDecimal(1);//100%
        for (RespGoodsStatisticsTwoLevel rep : respTwoLevel) {
            index++;
            BigDecimal countTemp = goodsCountTotal ==0 ? BigDecimal.ZERO : new BigDecimal(rep.getGoodsCountSum()).divide(new BigDecimal(goodsCountTotal),4, RoundingMode.HALF_EVEN);
            BigDecimal amountTemp = amountTotal.equals(BigDecimal.ZERO.setScale(2))?BigDecimal.ZERO:rep.getAmountSum().divide(amountTotal,4, RoundingMode.HALF_EVEN);
            rep.setAmountProportion(dt.format(amountTemp));
            rep.setGoodsCountProportion(dt.format(countTemp));
            if (index == respTwoLevel.size()){
                rep.setAmountProportion(dt.format(totalProportion.subtract(laseAmountProportion)));
                rep.setGoodsCountProportion(dt.format(totalProportion.subtract(laseCountProportion)));
            }else{
                laseAmountProportion = laseAmountProportion.add(amountTemp);
                laseCountProportion = laseCountProportion.add(countTemp);
            }
            if(goodsCountTotal==0){
                rep.setGoodsCountProportion(dt.format(0));
            }
            if(amountTotal.equals(BigDecimal.ZERO.setScale(2))){
                rep.setAmountProportion(dt.format(0));
            }
            respTwoLevelNew.add(rep);
        }
        sum.setRespTwoLevel(respTwoLevelNew);
        sum.setGoodsCountTotal(goodsCountTotal);
        sum.setAmountTotal(amountTotal);
        PageObj<RespGoodsStatisticsTwoLevelSum> result = PageObj.create(total, reqPageNum, pageSize, sum);
        return APIResponse.returnSuccess(result);
    }

    /**
     * 商品销售统计 -二级目录(导出)
     * @param map
     * @return
     */
    public APIResponse<?> getGoodsStatisticsByTwoLevelExport(@Param("map") Map<String, Object> map) {
        TbCommonParameter parameter = baseService.getParameterByKey("PLACE_LIMIT_DATE");
        String limitDate = parameter!=null && parameter.getParameterValue() != null ? parameter.getParameterValue() : "2017-04-29";
        map.put("placeLimitDate",limitDate);
        map.put("pageNum",null);
        map.put("pageSize",null);
        List<RespGoodsStatisticsTwoLevel> respTwoLevel = tbBizPlaceOrderManagerMapper.getGoodsStatisticsByTwoLevel(map);
        RespGoodsStatisticsTwoLevelSum sum = new RespGoodsStatisticsTwoLevelSum();
        Integer goodsCountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailCountSum(map);
        BigDecimal amountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailAmountSum(map);
        List<RespGoodsStatisticsTwoLevel> respTwoLevelNew = new ArrayList<>();
        DecimalFormat dt = new DecimalFormat("#0.00%");//设置格式化对象
        int index = 0;//下标
        BigDecimal laseAmountProportion = new BigDecimal(0);//最后实付金额比例
        BigDecimal laseCountProportion = new BigDecimal(0);//最后数量比例
        BigDecimal totalProportion = new BigDecimal(1);//100%
        for (RespGoodsStatisticsTwoLevel rep : respTwoLevel) {
            index++;
            BigDecimal countTemp = goodsCountTotal ==0 ? BigDecimal.ZERO : new BigDecimal(rep.getGoodsCountSum()).divide(new BigDecimal(goodsCountTotal),4, RoundingMode.HALF_EVEN);
            BigDecimal amountTemp = amountTotal.equals(BigDecimal.ZERO.setScale(2))?BigDecimal.ZERO:rep.getAmountSum().divide(amountTotal,4, RoundingMode.HALF_EVEN);
            rep.setAmountProportion(dt.format(amountTemp));
            rep.setGoodsCountProportion(dt.format(countTemp));
            if (index == respTwoLevel.size()){
                rep.setAmountProportion(dt.format(totalProportion.subtract(laseAmountProportion)));
                rep.setGoodsCountProportion(dt.format(totalProportion.subtract(laseCountProportion)));
            }else{
                laseAmountProportion = laseAmountProportion.add(amountTemp);
                laseCountProportion = laseCountProportion.add(countTemp);
            }
            if(goodsCountTotal==0){
                rep.setGoodsCountProportion(dt.format(0));
            }
            if(amountTotal.equals(BigDecimal.ZERO.setScale(2))){
                rep.setAmountProportion(dt.format(0));
            }
            respTwoLevelNew.add(rep);
        }
        sum.setRespTwoLevel(respTwoLevelNew);
        sum.setGoodsCountTotal(goodsCountTotal);
        sum.setAmountTotal(amountTotal);
        return APIResponse.returnSuccess(sum);
    }

    /**
     * 商品销售统计 -散客会员
     * @param map
     * @return
     */
    public APIResponse<?> getGoodsStatisticsByMember(@Param("map") Map<String, Object> map) {
        TbCommonParameter parameter = baseService.getParameterByKey("PLACE_LIMIT_DATE");
        String limitDate = parameter!=null && parameter.getParameterValue() != null ? parameter.getParameterValue() : "2017-04-29";
        map.put("placeLimitDate",limitDate);
        List<RespGoodsStatisticsMember> respMember = tbBizPlaceOrderManagerMapper.getGoodsStatisticsByMember(map);
        RespGoodsStatisticsMemberSum sum = new RespGoodsStatisticsMemberSum();
        Integer goodsCountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailCountSum(map);
        BigDecimal amountTotal = tbBizPlaceOrderManagerMapper.getGoodsDetailAmountSum(map);
        List<RespGoodsStatisticsMember> respMemberNew = new ArrayList<>();
        DecimalFormat dt = new DecimalFormat("#0.00%");//设置格式化对象
        int index = 0;//下标
        BigDecimal laseAmountProportion = new BigDecimal(0);//最后实付金额比例
        BigDecimal laseCountProportion = new BigDecimal(0);//最后数量比例
        BigDecimal totalProportion = new BigDecimal(1);//100%
        for (RespGoodsStatisticsMember rep : respMember) {
            index++;
            BigDecimal countTemp = goodsCountTotal ==0 ? BigDecimal.ZERO : new BigDecimal(rep.getGoodsCountSum()).divide(new BigDecimal(goodsCountTotal),4, RoundingMode.HALF_EVEN);
            BigDecimal amountTemp = amountTotal.equals(BigDecimal.ZERO.setScale(2))?BigDecimal.ZERO:rep.getAmountSum().divide(amountTotal,4, RoundingMode.HALF_EVEN);
            rep.setAmountProportion(dt.format(amountTemp));
            rep.setGoodsCountProportion(dt.format(countTemp));
            if (index == respMember.size()){
                rep.setAmountProportion(dt.format(totalProportion.subtract(laseAmountProportion)));
                rep.setGoodsCountProportion(dt.format(totalProportion.subtract(laseCountProportion)));
            }else{
                laseAmountProportion = laseAmountProportion.add(amountTemp);
                laseCountProportion = laseCountProportion.add(countTemp);
            }
            if(goodsCountTotal==0){
                rep.setGoodsCountProportion(dt.format(0));
            }
            if(amountTotal.equals(BigDecimal.ZERO.setScale(2))){
                rep.setAmountProportion(dt.format(0));
            }
            respMemberNew.add(rep);
        }
        sum.setRespMember(respMemberNew);
        sum.setGoodsCountTotal(goodsCountTotal);
        sum.setAmountTotal(amountTotal);
        return APIResponse.returnSuccess(sum);
    }

    /**
     * 商品库存操作流水记录
     * @param reqPlaceGoodsVo
     * @return
     */
    public APIResponse<?> insertGoodsOperateRecord(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        placeGoodsService.insertGoodsOperateRecord(reqPlaceGoodsVo);
        return APIResponse.returnSuccess();
    }

    /**
     * 获取商品类型及该商品类型下的库存
     * @param reqPlaceGoodsVo
     * @return
     */
    @Override
    public APIResponse<?> getGoodsTypeAndGoodsCount(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return APIResponse.returnSuccess(placeGoodsService.getGoodsTypeAndGoodsCount(reqPlaceGoodsVo.getPlaceId()));

    }

    @Override
    public APIResponse<?> insertGoodsCommonType(@Param("map") Map<String, Object> map) {
        return placeGoodsService.insertGoodsCommonType(map);
    }

    @Override
    public APIResponse<?> updateGoodsCommonType(@Param("map") Map<String, Object> map) {
        return placeGoodsService.updateGoodsCommonType(map);
    }

    @Override
    public APIResponse<?> goodsTypeCommonList(@Param("map") Map<String, Object> map) {
        return placeGoodsService.goodsTypeCommonList(map);
    }

    @Override
    public APIResponse<?> insertGoodsCommon(ReqPlaceGoodsCommonVo reqPlaceGoodsCommonVo) {
        return placeGoodsService.insertGoodsCommon(reqPlaceGoodsCommonVo);
    }

    @Override
    public APIResponse<?> updateGoodsCommon(ReqPlaceGoodsCommonVo reqPlaceGoodsCommonVo) {
        return placeGoodsService.updateGoodsCommon(reqPlaceGoodsCommonVo);
    }

    @Override
    public APIResponse<?> goodsCommonList(@Param("map") Map<String, Object> map) {
        return placeGoodsService.goodsCommonList(map);
    }

    @Override
    public APIResponse<?> getTypeCommonByParentLevel(@Param("map") Map<String, Object> map) {
        return APIResponse.returnSuccess(placeGoodsService.getTypeCommonByParentLevel(map));
    }

    @Override
    public APIResponse<?> insertInventoryRecordBatch(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return placeGoodsService.insertInventoryRecordBatch(reqPlaceGoodsVo);
    }

    @Override
    public APIResponse<?> checkGoodsInventoryBatch(ReqPlaceGoodsVo reqPlaceGoodsVo) {
        return placeGoodsService.checkGoodsInventoryBatch(reqPlaceGoodsVo);
    }

    @Override
    public APIResponse<?> updownGoods(ReqPlaceGoodsVo reqPlaceGoodVo) {
        return placeGoodsService.updownGoods(reqPlaceGoodVo);
    }

    @Override
    public APIResponse<?> getGoodsInfoByBarCode(ReqPlaceGoodsVo reqPlaceGoodVo) {
        return placeGoodsService.getGoodsInfoByBarCode(reqPlaceGoodVo);
    }

    @Override
    public APIResponse<?> insertGoodsAndList(@Param("map") Map<String, Object> map) {
        return placeGoodsService.insertGoodsAndList(map);
    }
}
